import FairyEditor = CS.FairyEditor;
import { Util } from "./Util";
const fs = CS.System.IO.File;
const director = CS.System.IO.Directory;
const Path = CS.System.IO.Path;
export class Setting
{
    /**资源目录路径*/public assetsPath:string;
    /**UI配置目录路径*/public uiJsonPath:string;
    /**UI代码目录路径*/public layoutPath:string;
    /**布局代码模板路径*/public layoutTemplatePath:string;
    /**项目名称*/public projName:string;
    public defaultCls:string;
    constructor()
    {
        const App = FairyEditor.App;
        let s= this;      
        let publishPath:string;
        publishPath = Path.Combine(App.project.settingsPath, "GYPublish.json");             
        s.projName = Path.GetFileName(Util.getParentDirectory(App.project.basePath));          
        s.assetsPath = Util.getParentDirectory(App.project.basePath);
        s.uiJsonPath = Path.GetFullPath("uiJson", s.assetsPath);
        s.layoutPath = Path.GetFullPath("src/gameModule/" + s.projName + "/ts/UI/layout", Util.getParentDirectory(s.assetsPath, 2));
        s.layoutTemplatePath = Path.Combine(App.project.settingsPath, "layout.ts-template");        
        s.defaultCls = "GYLite.GYUIComponent";
        if(!fs.Exists(s.layoutTemplatePath))
        {
            fs.WriteAllText(s.layoutTemplatePath, Setting.defaultLayoutTemp.replace("{TEMP_PATH}", s.layoutTemplatePath));
            console.log("不存在布局ts模板文件，自动创建!" + s.layoutTemplatePath); 
        }
        if(!fs.Exists(publishPath))
        {            
            let defaultSetting = {
                assetPath:s.assetsPath,
                uiJsonPath:s.uiJsonPath,
                layoutPath:s.layoutPath,
                layoutTemplatePath:s.layoutTemplatePath,
                defaultCls:s.defaultCls,
                projName:s.projName
            };            
            console.log("羔羊发布配置创建成功!" + publishPath);            
            fs.WriteAllText(publishPath, JSON.stringify(defaultSetting,null,4));
        }
        else
            console.log("存在羔羊发布配置!" + publishPath);

        
        //ui布局目录，没有则创建
        if(!director.Exists(s.uiJsonPath))
            director.CreateDirectory(s.uiJsonPath);
        //创建失败
        if(!director.Exists(s.uiJsonPath))
            console.error("插件无法使用，uiJson目录不存在！" + s.uiJsonPath);

        //ui布局代码目录，没有则创建
        if(!director.Exists(s.layoutPath))
            director.CreateDirectory(s.layoutPath)
        //创建失败
        if(!director.Exists(s.layoutPath))
            console.error("插件无法使用，资源目录不存在！" + s.layoutPath);
        
    }
    public getLayoutTS():string
    {
        let s = this;
        return fs.ReadAllText(s.layoutTemplatePath);
    }
    /**模板字符串
     * {TEMP_PATH} 模板文件路径
     * {MODULE} 模块名称
     * {COMPONENT} 组件名称
     * {PACKAGE} 包名称
     * {PROPERTY} ts属性定义
    */
    public static defaultLayoutTemp:string = `/**通过模板文件(G:\\羔羊框架\\2022最新工具引擎\\Git\\miniGames\\game_04\\assets\\allGame\\FGUIProject\\settings\\layout.ts-template)自动生成*/
module {MODULE}
{
    export class {COMPONENT}Layout
    {
        /**布局UI
         * @param xmlParser 配置解析器，请使用布局配置所属的winID获取
         * @param parent 布局的父级
         * @param justChidren 是否只布局子级内容，默认true，false则会创建组件容器
        */
        public static layout(xmlParser:edc.XMLParser, parent:GYLite.GYUIComponent, justChidren:boolean=true):GYLite.GYUIComponent
        {
            if(!xmlParser)return;
            let data:edc.IXMLData = xmlParser.getXMLData("{COMPONENT}.xml", "{PACKAGE}.json");
            if(justChidren)
            {
                xmlParser.instantiateChilds(data, parent, data);
                return null;
            }                
            return xmlParser.instantiate(data, parent);
        }
        
    }    
    export interface I{COMPONENT}
    {//{PROPERTY}		        
        [key:string]:any;
    } 
}`;
}